import { PdfJs, Spinner } from "../../core";

import PropertiesData from "./PropertiesData";
import { FC, ReactElement, useEffect, useState } from "react";

interface PropertiesLoaderProps {
  doc: PdfJs.PdfDocument;
  render(doc: PropertiesData): ReactElement;
}

const PropertiesLoader: FC<PropertiesLoaderProps> = ({ doc, render }) => {
  const [data, setData] = useState<PropertiesData>();

  useEffect(() => {
    doc
      .getMetadata()
      .then((meta) => {
        return Promise.resolve(meta);
      })
      .then((meta) => {
        return doc.getDownloadInfo().then((d) => {
          return Promise.resolve({
            fileName: meta.contentDispositionFilename || "",
            info: meta.info,
            length: d.length,
          });
        });
      })
      .then((response) => {
        setData(response);
      });
  }, []);

  return data ? (
    render(data)
  ) : (
    <div className="rpv-properties__loader">
      <Spinner />
    </div>
  );
};

export default PropertiesLoader;
